/**
 * 
 */
package rentReturnController;


import java.net.URL;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import constants.Constants;
import rentReturnEntity.CarDisplayableEntity;
import rentReturnEntity.TruckDisplayableEntity;
import rentReturnLogic.ReportLogic;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.ObservableMap;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.MapValueFactory;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;

/**
 * @author Sashiraj Dhanarajan
 *
 */
public class ReportController implements Initializable{

	public static int page=0;
	@FXML
	private ComboBox<String> reportFor;
	@FXML
	private TableColumn<Map<String, Integer>,Integer> economy;
	@FXML
	private TableColumn<Map<String, Integer>,Integer> compact;
	@FXML
	private TableColumn<Map<String, Integer>,Integer> midSize;
	@FXML
	private TableColumn<Map<String, Integer>,Integer> standard;
	@FXML
	private TableColumn<Map<String, Integer>,Integer> premium;
	@FXML
	private TableColumn<Map<String, Integer>,Integer> luxury;
	@FXML
	private TableColumn<Map<String, Integer>,Integer> suv;
	@FXML
	private TableColumn<Map<String, Integer>,Integer> van;
	@FXML
	private TableColumn<Map<String, Integer>,Integer> foot24;
	@FXML
	private TableColumn<Map<String, Integer>,Integer> foot15;
	@FXML
	private TableColumn<Map<String, Integer>,Integer> foot12;
	@FXML
	private TableColumn<Map<String, Integer>,Integer> boxTrucks;
	@FXML
	private TableColumn<Map<String, Integer>,Integer> cargoVan;
	@FXML
	private TableColumn<Map<String, Integer>,Integer> total;
	@FXML
	private TableColumn<Map<String, Integer>,Integer> carTotal;
	@FXML
	private TableColumn<Map<String, Integer>,Integer> truckTotal;
	@FXML
	private TableColumn<Map<String, Integer>,Integer> carTotal1;
	@FXML
	private TableColumn<Map<String, Integer>,Integer> truckTotal1;
	
	
	//-----------------------------------------------
	@FXML
	TableColumn<CarDisplayableEntity,Integer> economy1;
	@FXML
	TableColumn<CarDisplayableEntity,Integer> compact1;
	@FXML
	TableColumn<CarDisplayableEntity,Integer> midSize1;
	@FXML
	TableColumn<CarDisplayableEntity,Integer> standard1;
	@FXML
	TableColumn<CarDisplayableEntity,Integer> premium1;
	@FXML
	TableColumn<CarDisplayableEntity,Integer> luxury1;
	@FXML
	TableColumn<CarDisplayableEntity,Integer> suv1;
	@FXML
	TableColumn<CarDisplayableEntity,Integer> van1;
	@FXML
	TableColumn<CarDisplayableEntity,Integer> carTotal11;
	@FXML
	TableColumn<TruckDisplayableEntity,Integer> foot24long;
	@FXML
	TableColumn<TruckDisplayableEntity,Integer> foot15long;
	@FXML
	TableColumn<TruckDisplayableEntity,Integer> foot12long;
	@FXML
	TableColumn<TruckDisplayableEntity,Integer> boxTrucks1;
	@FXML
	TableColumn<TruckDisplayableEntity,Integer> cargoVan1;
	@FXML
	TableColumn<TruckDisplayableEntity,Integer> truckTotal11;

	@FXML
	TableView<TruckDisplayableEntity> truckrep;
	@FXML
	TableView<CarDisplayableEntity> carrep;
	
	//-----------------------------------------------------
	//@FXML
	//private TableColumn<Map<String, Integer>,Integer> total1;

	@FXML
	private TableView<Map<String, Integer>> carreport;
	@FXML
	private TableView<Map<String, Integer>> truckreport;
	@FXML
	private TableView<Map<String, Integer>> grantTotal;
	@FXML
	private Label message;
	@FXML
	private Text truckReportLabel;
	@FXML
	private Text carReportLabel;
	
	@FXML
	private Button exportToExcel;
	
	@FXML
	private Text truckLabel;
	
	@FXML
	private Text carLabel;
	
	
	private static ObservableMap<String,Integer> reportMap;
	
	
	
	public void displayTruckList()
	{
		ReportLogic rl = new ReportLogic();

ObservableList<TruckDisplayableEntity> listTruck= FXCollections.observableArrayList();
		

        foot24long.setCellValueFactory(new PropertyValueFactory<TruckDisplayableEntity, Integer>("foot24long"));
        foot15long.setCellValueFactory(new PropertyValueFactory<TruckDisplayableEntity, Integer>("foot15long"));
        foot12long.setCellValueFactory(new PropertyValueFactory<TruckDisplayableEntity, Integer>("foot12long"));
        boxTrucks1.setCellValueFactory((new PropertyValueFactory<TruckDisplayableEntity, Integer>("boxTrucks1")));
        cargoVan1.setCellValueFactory(new PropertyValueFactory<TruckDisplayableEntity, Integer>("cargoVan1"));
        truckTotal11.setCellValueFactory((new PropertyValueFactory<TruckDisplayableEntity, Integer>("truckTotal11")));
        
        ArrayList<TruckDisplayableEntity> truckDisp= new ArrayList<TruckDisplayableEntity>();
        try {
        	truckDisp= rl.fetchTruckOverallVehicleList();
        	for(int i=0;i<truckDisp.size();i++)
        	{
        		listTruck.add(truckDisp.get(i));
        	}
        	
        } catch (SQLException e) {
        	// TODO Auto-generated catch block
        	e.printStackTrace();
        }
                //CarDisplayableEntity c = new CarDisplayableEntity(1, 2, 3, 4, 5, 6, 7, 8, 9);
                
                truckrep.setItems(listTruck);
        	
	}
	
	public void displayCarList()
	{
		ReportLogic rl = new ReportLogic();
		
		
		ObservableList<CarDisplayableEntity> list= FXCollections.observableArrayList();
				
		     
		economy1.setCellValueFactory(new PropertyValueFactory<CarDisplayableEntity, Integer>("economy1"));
		compact1.setCellValueFactory(new PropertyValueFactory<CarDisplayableEntity, Integer>("compact1"));
		midSize1.setCellValueFactory(new PropertyValueFactory<CarDisplayableEntity, Integer>("midSize1"));
		standard1.setCellValueFactory((new PropertyValueFactory<CarDisplayableEntity, Integer>("standard1")));


		premium1.setCellValueFactory(new PropertyValueFactory<CarDisplayableEntity, Integer>("premium1"));
		luxury1.setCellValueFactory(new PropertyValueFactory<CarDisplayableEntity, Integer>("luxury1"));
		suv1.setCellValueFactory(new PropertyValueFactory<CarDisplayableEntity, Integer>("suv1"));
		van1.setCellValueFactory((new PropertyValueFactory<CarDisplayableEntity, Integer>("van1")));

		carTotal11.setCellValueFactory((new PropertyValueFactory<CarDisplayableEntity, Integer>("carTotal11")));


		ArrayList<CarDisplayableEntity> carDisp= new ArrayList<CarDisplayableEntity>();
		try {
			carDisp= rl.fetchCarOverallVehicleList();
			for(int i=0;i<carDisp.size();i++)
			{
				list.add(carDisp.get(i));
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		        //CarDisplayableEntity c = new CarDisplayableEntity(1, 2, 3, 4, 5, 6, 7, 8, 9);
		        
		        carrep.setItems(list);
			
			

	}
	
	@FXML
	public void fetchOverallVehicleReport(ActionEvent event)
	{
		carReportLabel.setVisible(false);
		truckReportLabel.setVisible(false);
		carrep.setVisible(false);
		truckrep.setVisible(false);
		if(reportFor.getValue().equals("Both"))
		{
			carReportLabel.setVisible(true);
			truckReportLabel.setVisible(true);
			carrep.setVisible(true);
			truckrep.setVisible(true);
			displayCarList();
			displayTruckList();
		}
		else if ( reportFor.getValue().equals("Car"))
			{	
				carrep.setVisible(true);
				carReportLabel.setVisible(true);
				displayCarList();
			}
		else if (reportFor.getValue().equals("Truck"))
			{
				truckReportLabel.setVisible(true);
				truckrep.setVisible(true);
			 	displayTruckList();
			}
		else
			System.out.println("Nothing selected");
	
	
	}
	
	
	
	
	
	/**
	 * This method is used for fetching report details based on the type of report selected
	 * @param event
	 * @pre The report button should be pressed
	 * @post Fetches report details based on the type of report selected
	 */
	@FXML
	public void fetchReportDetails(ActionEvent event){
		//fetchReturnReport()
		System.out.println("invoking fetch report details");
		carreport.setVisible(true);
		truckreport.setVisible(true);
		grantTotal.setVisible(true);
		exportToExcel.setVisible(true);
		truckLabel.setVisible(true);
		carLabel.setVisible(true);
		reportMap = null;
		ReportLogic logic = new ReportLogic();
		if(reportFor.getValue() != null && !reportFor.getValue().isEmpty()){
			if(reportFor.getValue().equalsIgnoreCase(Constants.DAILY_RENT_REPORT)){
				reportMap = logic.fetchRentReport();
				if(reportMap != null){
					populateTable();
				}
				else{
					message.setText("No value fetched");
				}
			}
			else if(reportFor.getValue().equalsIgnoreCase(Constants.DAILY_RETURN_REPORT)){
				reportMap = logic.fetchReturnReport();
				if(reportMap != null){
					populateTable();
				}
				else{
					message.setText("No value fetched");
				}
			}
		}
	}

	/**
	 * This method map the values fetched into the table
	 * @pre The type of report should be selected whether it is rent report or return report
	 * @post Display the table with values
	 */
	public void populateTable(){
		if(reportMap != null && reportMap.size() >0){
			if(!reportMap.containsKey("economy")){
				reportMap.put("economy", 0);
			}
			if(!reportMap.containsKey("premium")){
				reportMap.put("premium", 0);
			}
			if(!reportMap.containsKey("compact")){
				reportMap.put("compact", 0);
			}
			if(!reportMap.containsKey("midsize")){
				reportMap.put("midsize", 0);
			}
			if(!reportMap.containsKey("standard")){
				reportMap.put("standard", 0);
			}
			if(!reportMap.containsKey("luxury")){
				reportMap.put("luxury", 0);
			}
			if(!reportMap.containsKey("suv")){
				reportMap.put("suv", 0);
			}
			if(!reportMap.containsKey("van")){
				reportMap.put("van", 0);
			}
			if(!reportMap.containsKey("24-foot")){
				reportMap.put("24-foot", 0);
			}
			if(!reportMap.containsKey("15-foot")){
				reportMap.put("15-foot", 0);
			}
			if(!reportMap.containsKey("12-foot")){
				reportMap.put("12-foot", 0);
			}
			if(!reportMap.containsKey("box")){
				reportMap.put("box", 0);
			}
			if(!reportMap.containsKey("cargovan")){
				reportMap.put("cargovan", 0);
			}
			economy.setCellValueFactory(new MapValueFactory("economy"));
			compact.setCellValueFactory(new MapValueFactory("compact"));
			premium.setCellValueFactory(new MapValueFactory("premium"));
			midSize.setCellValueFactory(new MapValueFactory("midsize"));
			standard.setCellValueFactory(new MapValueFactory("standard"));
			luxury.setCellValueFactory(new MapValueFactory("luxury"));
			suv.setCellValueFactory(new MapValueFactory("suv"));
			van.setCellValueFactory(new MapValueFactory("van"));
			foot24.setCellValueFactory(new MapValueFactory("24-foot"));
			foot15.setCellValueFactory(new MapValueFactory("15-foot"));
			foot12.setCellValueFactory(new MapValueFactory("12-foot"));
			boxTrucks.setCellValueFactory(new MapValueFactory("box"));
			cargoVan.setCellValueFactory(new MapValueFactory("cargovan"));
			total.setCellValueFactory(new MapValueFactory("Total"));
			carTotal.setCellValueFactory(new MapValueFactory("CarTotal"));
			truckTotal.setCellValueFactory(new MapValueFactory("TruckTotal"));
			carTotal1.setCellValueFactory(new MapValueFactory("CarTotal"));
			truckTotal1.setCellValueFactory(new MapValueFactory("TruckTotal"));
		}
		carreport.getItems().setAll(reportMap);
		truckreport.getItems().setAll(reportMap);
		grantTotal.getItems().setAll(reportMap);
		/*}
		else{
			moveTable.getItems().clear();
		}*/
	}
	/**
	 * This method is used for exporting the table values into excel
	 * @param event
	 * @pre This method is invoked when export excel button is pressed 
	 * @post The values are exported to excel
	 */
	@FXML
	public void exportToExcel(ActionEvent event){
		ReportLogic logic = new ReportLogic();
		if(reportFor.getValue() != null && !reportFor.getValue().isEmpty()){
			if(reportMap != null && reportMap.size() >0){
				logic.exportToExcel(reportMap,reportFor.getValue());
				message.setText("Exported to Excel successfully");
				message.setTextFill(Color.GREEN);
			}
			else{
				message.setText("No value to export");
			}
		}
		else{
			message.setText("Please select value to generate report");
		}

	}


	@Override
	public void initialize(URL location, ResourceBundle resources) {
		// TODO Auto-generated method stub
		if(page==1)
		{
			reportFor.setItems(FXCollections.observableArrayList("Car", "Truck","Both"));
		}
		
	}
}
